// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Plugin.highcharts");
yepjs.require("YBA.Data.Statistics");


// 安全审计 - 历史在线情况统计
yepjs.define("onlineHistory", {

    NS: "YBA.Logic.Statistics",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                Biz.fn.getOnlineHistory(Biz.vars.data);
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
            },
            //  公共变量
            vars: {
                // 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                //暂定空参
                data:{}
            },
            // 页面元素
            el: {
                jhistoryList:$("#j-historyList"),
                jstatisticsList:$("#j-statisticsList"),
                jrecordList:$("#j-recordList")
            },
            // 功能函数集合
            fn: {
                //获取历史在线情况统计信息
                getOnlineHistory:function(data){
                    YBA.Data.Statistics.customerOnlineHistory(data,function(json){
                        if(json.code == 200) {
                            Biz.el.jhistoryList.html(YBA.Common.ysmart(Biz.vars.tpl.historyTpl, json.historyOnline));
                            Biz.el.jstatisticsList.html(YBA.Common.ysmart(Biz.vars.tpl.statisticsTpl, json.historyOnlineStatistics));
                            Biz.el.jrecordList.html(YBA.Common.ysmart(Biz.vars.tpl.recordTpl, json.historyOnlineRecord));
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc})
                        }
                    })
                },
                //离线原因分析弹窗
                offlineForm:function(json){
                    var popform = new YBA.Plugin.popform({
                        title:"离线原因分析",
                        className:"ui-dialog-offline",
                        content:YBA.Common.ysmart(Biz.vars.tpl.offlineTpl,json || {}),
                        width:564,
                        destroy:true,
                        onHide: function () {
                            // 隐藏弹窗时要执行的回调函数
                        },
                        onShow:function(){
                            // 显示弹窗时要执行的回调函数
                        }
                    }).show();
                },
                //历史在线情况弹窗
                onlineHistoryForm:function(json){
                    var popform = new YBA.Plugin.popform({
                        title:"历史在线情况",
                        className:"ui-dialog-onlineHistory",
                        content:YBA.Common.ysmart(Biz.vars.tpl.onlineHistoryTpl,json || {}),
                        width:864,
                        destroy:true,
                        onHide: function () {
                            // 隐藏弹窗时要执行的回调函数
                        },
                        onShow:function(){
                            // 显示弹窗时要执行的回调函数
                            $('#j-lineBasic').highcharts({
                                title: {
                                    text: '历史在线情况',
                                    x: -20 //center
                                },
                                xAxis: {
                                    categories: json.time
                                },
                                credits:{
                                    text: '',
                                    href: ''
                                },
                                yAxis: {
                                    title: {
                                        text: '在线时长 (小时)'
                                    },
                                    plotLines: [{
                                        value: 0,
                                        width: 1,
                                        color: '#808080'
                                    }]
                                },
                                tooltip: {
                                    valueSuffix: '小时'
                                },
                                legend: {
                                    layout: 'vertical',
                                    align: 'right',
                                    verticalAlign: 'middle',
                                    borderWidth: 0
                                },
                                series: [{
                                    name: '当前终端',
                                    color: '#89A54E',
                                    type: 'column',
                                    data: json.onlineHour
                                }]
                            });
                        }
                    }).show();
                }
            },
            // 初始化时执行的所有事件
            event: {
                //离线原因分析
                offlineReasonToday:function(){
                    Biz.el.jrecordList.delegate(".j-offline","click", function() {
                        var t = $(this), tr = t.closest("tr"),id = tr.attr("data-id");
                        var data = {
                            "terminalId":id
                        }
                        YBA.Data.Statistics.terminalOfflineReasonHistory(data,function(json){
                            if(json.code == 200) {
                                Biz.fn.offlineForm(json.historyOfflineReason);
                            }else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                        })
                    });
                },
                //查看历史在线情况
                terminalOnlineHistory:function(){
                    Biz.el.jstatisticsList.delegate(".j-onlineHistory","click", function() {
                        var t = $(this), tr = t.closest("tr"),id = tr.attr("data-id");
                        var data = {
                            "terminalId":id
                        }
                        YBA.Data.Statistics.terminalOnlineHistory(data,function(json){
                            if(json.code == 200) {
                                var caseData = {
                                    "onlineHour":[],
                                    "time":[]
                                };
                                for(i=0;i<json.historyOnlineCase.length;i++){
                                    caseData.onlineHour[i] = parseFloat(json.historyOnlineCase[i].onlineHour);
                                    caseData.time[i] = json.historyOnlineCase[i].time;
                                }
                                Biz.fn.onlineHistoryForm(caseData);
                            }else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                        })
                    });
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
